using System;
using System.Collections.Generic;
using System.Text;
using Xyne.Actions;
using Xyne.Regions;
using Xyne.Utilities;
using GarageGames.Torque.Core;
using Microsoft.Xna.Framework;
using GarageGames.Torque.T2D;
using Microsoft.Xna.Framework.Net;

namespace Xyne.Templates.BattleEffects
{
    public class ThrownPhoenixEffect : BattleEffect
    {
        #region Constants
        private const string ObjectName = "PhoenixEffect";
        #endregion

        #region Members
        private ProjectileAction thrownObjectAction;
        private BattleRegionImpl battleRegion;
        private GameObjects target;
        #endregion

        #region Properties
        public ProjectileAction Action
        {
            get
            {
                return thrownObjectAction;
            }
        }
        #endregion

        #region Template
        private static ThrownPhoenixEffect template;

        public static ThrownPhoenixEffect Template
        {
            get
            {
                if (template == null)
                {
                    template = new ThrownPhoenixEffect();
                }

                return template;
            }
        }
        #endregion

        #region Constructor
        public ThrownPhoenixEffect()
        {
            IsTemplate = true;
            Name = ObjectName;

            CreateWithWorldLimit = true;

            AnimationData = AnimationManager.Instance.GetAnimationData("phoenixANIM");
            Layer = Game.EffectLevel;
            ObjectType = TorqueObjectDatabase.Instance.GetObjectType(Name);

            Size = new Vector2(75, 30);
            EffectType = BattleEffectType.ThrownPhoenixEffect;
        }

        public void Init(ProjectileAction inThrownObjectAction, BattleRegionImpl inBattleRegion, GameObjects inTarget)
        {
            AnimationData.AnimationDuration = 1;

            thrownObjectAction = inThrownObjectAction;
            battleRegion = inBattleRegion;
            target = inTarget;
        }
        #endregion

        #region Overrides
        public override void StartEffect()
        {
            base.StartEffect();
            Vector2 targetPosition = battleRegion.GetPosition(target);
            Vector2 originPosition = battleRegion.GetPosition(thrownObjectAction.ActualOrigin);

            FlipX = targetPosition.X < originPosition.X;
            if (FlipX)
            {
                Physics.Velocity = new Vector2(-thrownObjectAction.Speed, 0);
            }
            else
            {
                Physics.Velocity = new Vector2(thrownObjectAction.Speed, 0);
            }

            WarpToPosition(originPosition, 0.0f);
            CollisionsEnabled = true;
            Collision.CollidesWith = battleRegion.GetTargetObjectType(target);
            Collision.OnCollision = new T2DOnCollisionDelegate(OnCollision);

            OnAnimationEnd = new OnAnimationEndDelegate(AnimationEnded);
        }
        #endregion

        #region Serialization
        public void Update(PacketReader reader)
        {
            Position = reader.ReadVector2();
            Physics.Velocity = reader.ReadVector2();
        }

        public override void Serialize(PacketWriter writer)
        {
            writer.Write(Position);
            writer.Write(Physics.Velocity);
        }
        #endregion

        #region Events
        public void OnCollision(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolveCollisionDelegate,
            ref T2DCollisionMaterial physicsMaterial)
        {
            battleRegion.ModifyHealth(target, thrownObjectAction.HealthChange);
            ourObject.MarkForDelete = true;

            BattleEffectManager.Instance.RemoveBattleEffect(ourObject as BattleEffect);

            
            if (thrownObjectAction.OnCollision != null && thrownObjectAction.Level >= thrownObjectAction.OnCollision.MinValue)
            {
                ThrownPhoenixEffect tempEffect = ourObject as ThrownPhoenixEffect;
                thrownObjectAction.OnCollision.Action.Level = tempEffect.Action.Level-1;
                thrownObjectAction.OnCollision.Action.OnCollision = thrownObjectAction.OnCollision;
                ActionManager.Instance.ActionActivated(thrownObjectAction.OnCollision.Action);
            }
        }

        public void AnimationEnded()
        {
            SetAnimationFrame(0);
            ResumeAnimation();
        }
        #endregion
    }
}
